<?php
/**
 * Created by PhpStorm.
 * User: ysk
 * Date: 2021/6/18
 * Time: 16:04
 */

namespace api\base;

use api\base\BaseApiController;
use Yii;
use yii\base\Action;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;

class BaseApiAction extends Action {

    public function behaviors()
    {
        $behaviors = parent::behaviors();

        return ArrayHelper::merge(
            $behaviors,
            [
                'corsFilter' => BaseApiController::$corsFilterSetting,
            ]
        );
    }

    /**
     * 成功
     * @param $data
     * @param $msg
     * @return ResponseApi
     */
    public static function respondSuccess($data='',$msg=null){

        return ResponseApi::make($data, $msg, null, null, '200');
    }

    /**
     * 自定义响应
     * @param $data
     * @param $msg
     * @param $sub_msg
     * @param $sub_code
     * @param $code
     * @return ResponseApi
     */
    public static function respond(
        $data,
        string $msg = 'OK',
        ?string $sub_msg = null,
        ?string $sub_code = null,
        ?string $code = '200'
    ) {
        return ResponseApi::make($data, $msg, $sub_msg, $sub_code, $code);
    }

    /**
     * 跳转
     *
     * @param string $url    地址
     * @param string $method 请求方法,GET,POST
     * @param array  $params 参数
     *
     * @return ResponseApi
     */
    public static function respondRedirect(string $url, string $method = 'GET', array $params = [])
    {
        return ResponseApi::redirect($url, $method, $params);
    }


    /**
     * 失败
     * @param $msg
     * @param $sub_msg
     * @param $sub_code
     * @param $code
     * @return ResponseApi
     */

    public static function respondFailure($msg, $sub_msg=null, $sub_code=null, $code='400'){

        return ResponseApi::make(null, $msg,$sub_code , $sub_msg, $code);

    }

}
